package com.nextbus.parser;


import java.util.ArrayList;
import java.util.HashMap;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.nextbus.model.Prediction;



public class BusLocationParser extends DefaultHandler{
	private Prediction prediction;
	String epochTime,seconds,minutes,isDeparture,affectedByLayover,dirTag,vehicle,block,tripTag;
	String stoptag;
	private String dirTagid;
	private ArrayList<Prediction>preList =null;
    private ArrayList<String>stopTagList = new ArrayList<String>();	
	private ArrayList<ArrayList<Prediction>>preLists = new ArrayList<ArrayList<Prediction>>();
	
	public BusLocationParser(String dirTagid) {
		super();
		this.dirTagid=dirTagid;
	}

	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
	}

	public void endDocument() throws SAXException {
		System.out.println("解析结束");
		System.out.println("有预测信息的站点个数"+preLists.size());
		super.endDocument();
	}

	public void endElement(String uri, String localName, String qName)
			throws SAXException {
//		if(localName.equals("direction")){
//			System.out.println("direction");
//		}
	  if(localName.equals("predictions"))
		{   
			//System.out.println("preList的大小为"+preList.size());
			if(preList.size()>0)
			{		
				stopTagList.add(stoptag);	
				preLists.add(preList);
			}
		}
		super.endElement(uri, localName, qName);
	}

	public void startDocument() throws SAXException {
//		stopTagList = new ArrayList<String>();
//		preLists=new ArrayList<ArrayList<Prediction>>();
		super.startDocument();
		System.out.println("开始stop预测信息的解析");
	}

	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if(localName.equals("predictions"))
		{
			preList=new ArrayList<Prediction>();
			stoptag=attributes.getValue("stopTag");
		}
		if(localName.equals("prediction")&&attributes.getValue("dirTag").equals(dirTagid))
		{	
			epochTime=attributes.getValue("epochTime");
			seconds=attributes.getValue("seconds");
			minutes=attributes.getValue("minutes");
			isDeparture=attributes.getValue("isDeparture");
			affectedByLayover=attributes.getValue("affectedByLayover");
			dirTag=attributes.getValue("dirTag");
			vehicle=attributes.getValue("vehicle");
			block=attributes.getValue("block");
			tripTag=attributes.getValue("tripTag");
			prediction=new Prediction(epochTime,seconds,minutes, isDeparture,affectedByLayover,dirTag,vehicle,block,tripTag);  
		    preList.add(prediction);
		}
		super.startElement(uri, localName, qName, attributes);
	}

	//获得信息用于将数据保存到routeConfig中
	public ArrayList<Prediction> getPreList() {
		System.out.println(preList.size());
		return preList;
	}

	public ArrayList<String> getStopTagList() {
		return stopTagList;
	}

	public void setStopTagList(ArrayList<String> stopTagList) {
		this.stopTagList = stopTagList;
	}

	public ArrayList<ArrayList<Prediction>> getPreLists() {
		return preLists;
	}

	public void setPreLists(ArrayList<ArrayList<Prediction>> preLists) {
		this.preLists = preLists;
	}
   
}
